package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.apache.ibatis.annotations.Param;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("/showUsername")
    public Result showUsername(){
//        在域中取User
        try {
            org.springframework.security.core.userdetails.User user= (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //获取当前登录（认证）用户的用户名
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 分页展示所有用户
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findUserByPage")
    public PageResult findUserByPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findUserByPage(queryPageBean);
    }

    /**
     * 新增用户
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, @Param("roleIds") Integer[] roleIds){
        try {
            Result result = userService.add(user, roleIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }
    /**
     * 修改密码
     * @param map
     * @return
     */
    @RequestMapping("/modify")
    public Result modify(@RequestBody Map map){
        try {
            Result result = userService.modifyPassword(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_PASSWORD_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }

    /**
     * 回显编辑数据
     * @param id
     * @return
     */
    @RequestMapping("/findUserById")
    public Result findUserById(Integer id){
        try {
            Map map = userService.findUserById(id);
            return new Result(true,"",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DATA_EXCEPTION);
        }
    }

    /**
     * 编辑数据
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Result result = userService.deleteUser(id);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DEL_USER_FAIL,MessageConstant.DATA_EXCEPTION);
        }
    }
}
